//    Copyright (C) Kherty.  All rights reserved.
using System.Diagnostics.Contracts;
using OpenLS.Core.Serialization;
using OpenLS.Drawing.Internals;

namespace OpenLS.Drawing
{
    public class PositiveSize2D : IOfficeXmlSerializableAs //\\replace with Size
    {
        private double cx;
        private double cy;

        public PositiveSize2D()
        {
        }

        public PositiveSize2D(double w, double h)
        {
            Width = w;
            Height = h;
        }


        public double Width
        {
            get { return cx; }
            set
            {
                Contract.Requires(value >= 0);
                cx = value;
            }
        }

        public double Height
        {
            get { return cy; }
            set
            {
                Contract.Requires(value >= 0);
                cy = value;
            }
        }

        #region IOfficeXmlSerializableAs Members

        void IOfficeXmlSerializableAs.ReadXmlAs(string name, ReadContext context)
        {
            using (var c = context.Read(name))
            {
                Width = EmuConverter.FromEmu(c.GetInteger64("cx"));
                Height = EmuConverter.FromEmu(c.GetInteger64("cy"));
            }
        }

        void IOfficeXmlSerializableAs.WriteXmlAs(string name, WriteContext context)
        {
            using (WriteContext c = context.Write(name))
            {
                c.SetInteger64("cx", EmuConverter.ToEmu(Width));
                c.SetInteger64("cy", EmuConverter.ToEmu(Height));
            }
        }

        #endregion
    }
}